package search;

/**
 * 插值查找
 */
public class InterpolationSearch {
    public static void main(String[] args) {
        int[] arr = new int[100];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }
        System.out.println(interpolationSearch(arr, 0, arr.length - 1, 99));
    }

    public static int interpolationSearch(int[] arr, int left, int right, int value) {
        if (left > right || value < arr[0] || value > arr[arr.length - 1]) {
            //没有这个数
            return -1;
        }
        int mid = left + (right - left) * (value - arr[left]) / (arr[right] - arr[left]);
        if(arr[mid] > value){
            return interpolationSearch(arr, left, mid - 1, value);
        }else if(arr[mid] < value){
            return interpolationSearch(arr, mid + 1, right, value);
        }else {
            return mid;
        }
    }
}
